using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using TypeDataset;

namespace DataAccessor
{
	public class DBConnectionService
	{
#region private member

		private const String _conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ";
		private static DBConnectionService _Instance = new DBConnectionService();
		private OleDbConnection _Connection;
		private const String _ApplicationDir = "MaskApplication";
		private String _connectionString = "";
		private OleDbDataAdapter _MaskViewAdapter = null;
		private DBConnectionService()
		{
			_Connection = new OleDbConnection(GetConnectionString());
		}
#endregion

		public static DBConnectionService Instance()
		{
			return _Instance;
		}

		private String GetConnectionString()
		{
			if (_connectionString == "")
			{
				String AppDir = Environment.GetEnvironmentVariable(_ApplicationDir);
				Debug.Assert(AppDir != null);
				_connectionString = _conString + AppDir + "/db/maskMgr.mdb";
			}
			return _connectionString;
		}

		public bool UpdateMaskInfo(MaskMainView.BaseMaskViewDataTable table)
		{
			if (_MaskViewAdapter != null)
			{
				_MaskViewAdapter.Update(table);
				return true;
			}
			else
				return false;
		}
		public bool UpdateMaskInfo(MaskMainView.BaseMaskViewRow row)
		{
			if (_MaskViewAdapter != null)
			{
				_MaskViewAdapter.Update(new DataRow[] { row });
				return true;
			}
			else
				return false;

		}
		public MaskMainView.BaseMaskViewDataTable GetMaskViewTable(string sql)
		{
			MaskMainView.BaseMaskViewDataTable table = new MaskMainView.BaseMaskViewDataTable();

			_MaskViewAdapter = new OleDbDataAdapter(sql, _Connection);
			OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(_MaskViewAdapter);
			commandBuilder.QuotePrefix = "[";
			commandBuilder.QuoteSuffix = "]";
			_MaskViewAdapter.Fill(table);
			return table;
		}
		public DataTable GetDataTable(string sql, DataTable table)
		{
			OleDbDataAdapter dAdapter = new OleDbDataAdapter(sql, _Connection);
			dAdapter.Fill(table);
			return table;
		}
	}
}
